﻿namespace Vibstudio.NetConfigurator.IoC.Configuration
{
    using System.Collections.Specialized;
    using System.Configuration;
    using System.Diagnostics;

    public class NcConfigurationManager
    {
        private static readonly object lockingObject = new object();
        private static NcSettingsSection _settings;

        [DebuggerNonUserCode]
        public static NameValueCollection AppSettings
        {
            get
            {
                lock (lockingObject)
                {
                    if (_settings == null)
                    {
                        _settings = new NcSettingsSection();
                        _settings.GetSettings();
                    }
                }
                return _settings;
            }
        }

        private class NcSettingsSection : NameValueCollection
        {
            [DebuggerNonUserCode]
            public void GetSettings()
            {
                if (base.Count == 0)
                {
                    NameValueCollection appSettings = ConfigurationManager.AppSettings;
                    for (int i = 0; i < appSettings.Count; i++)
                    {
                        string key = appSettings.Keys[i];

                        if (base[key] == null)
                        {
                            base.Add(key, appSettings[i]);
                        }
                    }
                }
            }
        }
    }
}
